package com.zyzd.baseprojectofvolly.utils.volley;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.zyzd.baseprojectofvolly.MyApplication;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * Created by 91986 on 2016/7/26.
 */
public class VolleyHelper {

    public static RequestQueue mRequestQueue = Volley.newRequestQueue(MyApplication.getContext());

    private VolleyHelper() {
    }

    //--------------------------------------- String ---------------------------------------

    /**
     * 不带对话框，String总请求
     *
     * @param tag 上下文
     */
    public static void stringRequest(int method, final String url, Object tag, Response.Listener listener, Response.ErrorListener errorListener, final Map<String, String> params) {
        StringRequest request = new StringRequest(method, url, listener, errorListener) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                //TODO 需要时，使用自定义创建的header
                return super.getHeaders();
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {//重写了译码方式，可避免乱码
                String parsed;
                try {
                    parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers, "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    parsed = new String(response.data);
                }
                return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }
        };

        addRequest(request, tag);

    }

    /**
     * 不带对话框，String总请求
     *
     * @param method
     * @param url
     * @param tag
     * @param listener
     * @param errorListener
     * @param params
     * @return
     */
    public static StringRequest stringRequestHasResponse(int method, final String url, Object tag, Response.Listener listener, Response.ErrorListener errorListener, final Map<String, String> params) {
        StringRequest request = new StringRequest(method, url, listener, errorListener) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                //TODO 需要时，使用自定义创建的header
                return super.getHeaders();
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {//重写了译码方式，可避免乱码
                String parsed;
                try {
                    parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers, "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    parsed = new String(response.data);
                }
                return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }
        };

        addRequest(request, tag);
        return request;

    }

    /**
     * 不带对话框，String get请求
     *
     * @param tag 上下文
     */
    public static void stringRequest(String url, Object tag, Response.Listener listener, Response.ErrorListener errorListener) {
        stringRequest(Request.Method.GET, url, tag, listener, errorListener, null);
    }


    /**
     * 不带对话框，String post请求
     *
     * @param tag 上下文
     */
    public static void stringRequest(String url, Object tag, Map<String, String> params, Response.Listener listener, Response.ErrorListener errorListener) {
        stringRequest(Request.Method.POST, url, tag, listener, errorListener, params);
    }


    //--------------------------------------- gson ---------------------------------------

    /**
     * 不带对话框，gson总请求
     *
     * @param tag   上下文
     * @param clazz 解析对象
     */
    public static <T> void gsonRequest(int method, String url, Map<String, String> params, Object tag, Class<T> clazz, Response.Listener listener, Response.ErrorListener errorListener) {
        GsonRequest<T> gsonRequest = new GsonRequest<>(method, url, params, clazz, listener, errorListener);
        addRequest(gsonRequest, tag);
    }

    /**
     * 不带对话框，gson get请求
     *
     * @param tag   上下文
     * @param clazz 解析对象
     */
    public static <T> void gsonRequest(String url, Object tag, Class<T> clazz, Response.Listener listener, Response.ErrorListener errorListener) {
        gsonRequest(Request.Method.GET, url, null, tag, clazz, listener, errorListener);
    }

    /**
     * 不带对话框， gson post请求
     *
     * @param tag    上下文
     * @param clazz  解析对象
     * @param params 请求参数列表
     */
    public static <T> void gsonRequest(String url, Map<String, String> params, Object tag, Class<T> clazz, Response.Listener listener, Response.ErrorListener errorListener) {
        gsonRequest(Request.Method.POST, url, params, tag, clazz, listener, errorListener);
    }

    /**
     * 添加请求
     *
     * @param request
     * @param tag
     */
    public static void addRequest(Request request, Object tag) {
        if (tag != null) {
            request.setTag(tag);
        }
        mRequestQueue.add(request);
    }

    /**
     * 当主页面调用协议 在结束该页面调用此方法
     *
     * @param tag
     */
    public static void cancelAll(Object tag) {
        mRequestQueue.cancelAll(tag);
    }
}
